package site.assad.service;

import org.springframework.stereotype.Service;
import site.assad.model.User;

import java.util.Arrays;
import java.util.List;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2018/1/10 21:21
 * Description: 模拟业务层
 */
@Service
public class UserService {

    //模拟数据库中的储存数据
    private List<User> userList = Arrays.asList(new User(1,"assad",20),
                                                new User(2,"Vancy",22),
                                                new User(3,"John",18));

    public User getUser(int userId){
        for(User user : userList){
            if(user.getUserId() == userId)
                return user;
        }
        return null;
    }

    public void addUser(User user){
        if(userList.contains(user))
            userList.add(user);
    }

    public void deleteUser(User user){
        if(userList.contains(user))
            userList.remove(user);
    }

    public void updateUser(User user){
        for(User u : userList){
            if(u.getUserId() == user.getUserId())
                userList.set(u.getUserId(),user);
        }
    }

    public List<User> getUserList(){
        return userList;
    }



}
